package hai.wom.org.qyer_destination.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import hai.wom.org.qyer_destination.R;
import hai.wom.org.qyer_destination.adapter.MyAdapter_Hot;
import hai.wom.org.qyer_destination.bean.HotCity;
import hai.wom.org.qyer_destination.callback.ICallback;
import hai.wom.org.qyer_destination.callback.IIndex;
import hai.wom.org.qyer_destination.callback.IList;
import hai.wom.org.qyer_destination.utils.HttpUtils;

/**
 * Created by Administrator on 2017/1/12 0012.
 */

public class Fragment_Hot extends Fragment implements AdapterView.OnItemClickListener,IList {
    private List<HotCity.DataEntity> list;
    private MyAdapter_Hot adapter_hot;
    //    private String path = "http://open.qyer.com/lastminute/conf/destination?client_id=qyer_discount_androi&client_secret=227097da1d07a2a9860f&track_user_id=&track_deviceid=352284042226724&track_app_version=2.0.4&track_app_channel=qq&track_device_info=hlte&track_os=Android4.4.2&app_installtime=1483531797801&size=720x1242&ra_referer=app_home&lat=39.084881&lon=117.199403";
    private ListView lv;
    private IIndex iCb;

    public void setICB(IIndex cb) {
        this.iCb = cb;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_hot_layout, container, false);
        lv = (ListView) view.findViewById(R.id.lv_fg_hot);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        list = new ArrayList<>();
        adapter_hot = new MyAdapter_Hot(list, getContext());
        lv.setAdapter(adapter_hot);
        lv.setOnItemClickListener(this);


    }

//    private void loadData() {
//        new HttpUtils(new ICallback() {
//            @Override
//            public void getData(String result) {
//                try {
//                    JSONObject jsonObject = new JSONObject(result);
//                    JSONArray array = jsonObject.getJSONArray("data");
//                    for (int i = 0; i < array.length(); i++) {
//                        JSONObject js = array.getJSONObject(i);
//                        HotCity.DataEntity data = new HotCity.DataEntity();
//                        data.setName(js.getString("name"));
//                        list.add(data);
//                    }
//
//                    adapter_hot.notifyDataSetChanged();
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//
//            }
//        }).execute(path);
//    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        iCb.sendIndex(position);
    }

    @Override
    public void sendData(List<HotCity.DataEntity> datas) {
        list.clear();
        list.addAll(datas);
        adapter_hot.notifyDataSetChanged();
    }
}
